package com.interest.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.interest.authorization.annotation.Authorization;
import com.interest.base.entity.ModelBean;
import com.interest.base.utils.ResponseUtils;
import com.interest.modules.task.entity.Task;
import com.interest.modules.task.entity.TaskHelper;
import com.interest.modules.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/api/task")
public class ApiTaskController {
    @Autowired
    private TaskService taskService;

    @RequestMapping("/delete")
    @Authorization
    @ResponseBody
    public ModelBean delete(@RequestParam(value = "id") int id) {
        int code = taskService.delete(id);
        JSONObject data = new JSONObject();
        data.put("result", code);
        return new ModelBean(data);
    }

    //    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequestMapping("/add")
//    @Authorization
    @ResponseBody
    public ModelBean add(@RequestParam(value = "task") String task) {
        int code = taskService.add(TaskHelper.from(task));
        JSONObject data = new JSONObject();
        data.put("result", code);
        return new ModelBean(data);
    }

    @RequestMapping("/detail")
    @Authorization
    @ResponseBody
    public ModelBean getDetail(@RequestParam(value = "id") int id) {
        Task task = taskService.getDetail(id);
        JSONObject data = new JSONObject();
        data.put("result", task);
        return new ModelBean(data);
    }

    @RequestMapping("/list")
//    @Authorization
    @ResponseBody
    public ModelBean getList() {
        List<Task> list = taskService.getList();
        return ResponseUtils.generateSuccess(list);
    }

    @RequestMapping("/update")
    @Authorization
    @ResponseBody
    public ModelBean update(@RequestParam(value = "task") String task) {
        int code = taskService.update(TaskHelper.from(task));
        JSONObject data = new JSONObject();
        data.put("result", code);
        return new ModelBean(data);
    }

    @RequestMapping("/complete")
    @Authorization
    @ResponseBody
    public ModelBean complete(@RequestParam(value = "id") int id) {
        int code = taskService.complete(id);
        JSONObject data = new JSONObject();
        data.put("result", code);
        return new ModelBean(data);
    }

    @RequestMapping("/activate")
    @Authorization
    @ResponseBody
    public ModelBean activate(@RequestParam(value = "id") int id) {
        int code = taskService.activate(id);
        JSONObject data = new JSONObject();
        data.put("result", code);
        return new ModelBean(data);
    }
}
